﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;

using Ivy.Common.Framework.GeneralUtility;
using Ivy.UserRegistration.IBusinessLogic;
using Ivy.UserRegistration.BusinessLogic;
using Ivy.UserRegistration.Entity;
using HSMPWeb.ViewEntity;

namespace HSMPWeb.UserControls
{
    public partial class RegistrationControl : System.Web.UI.UserControl
    {
        #region  Variable & constant
        AntiSpamUtility antiSpam = new AntiSpamUtility();
        const string constCultureInfo = "CultureInfo";
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["UserContext"] == null)
                {
                    lblPageHeader.Text = "Registration";
                }
                else
                {
                    lblPageHeader.Text = "Edit Profile";
                    IUserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
                    User lUser = new User();
                    UserContext UserSession = new UserContext();
                    UserSession = (UserContext)Session["UserContext"];
                    lUser = lUserRegistrationBus.GetUserByLoginID(UserSession.CurrentUser.UserLoginID);
                    txtFirstName.Text = lUser.UserFirstName;
                    txtLastName.Text = lUser.UserLastName;
                    txtLoginId.Text = lUser.UserLoginID;
                    txtLoginId.Enabled = false;
                    txtPassword.Text = StringUtility.DecryptString(lUser.UserPassword);
                    txtPassword.Enabled = false;
                    txtConfirmPassword.Text = StringUtility.DecryptString(lUser.UserPassword);
                    txtConfirmPassword.Enabled = false;
                    txtEmailId.Text = lUser.UserPrimaryEmailID;

                    UserProfile lUserProfile = new UserProfile();
                    lUserProfile = lUserRegistrationBus.GetUserProfileByID(lUser.UserKey.ToString());

                    txtAlternateEmailId.Text = lUserProfile.UserAlternateEmailID;
                    ddlGender.Text = lUserProfile.UserSex;
                    txtDOB.Text = lUserProfile.UserDateOfBirth.ToShortDateString().ToString();
                    txtContactNo.Text = lUserProfile.UserContactNumber;
                    txtAddress.Text = lUserProfile.UserContactAddress;
                }

                if (!IsPostBack)
                {
                    hidCorrectAnswer.Value = antiSpam.GenerateMathFormula();
                    lblAntiSpam.Text = antiSpam.MathFormula;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                //Need to display the exception message to the user or log it in application log.
            }
        }

        protected void btnAvailibity_Click(object sender, EventArgs e)
        {
            IUserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
            if (string.IsNullOrEmpty(txtLoginId.Text.Trim()))
            {
                lblAvailibity.ForeColor = System.Drawing.Color.Red;
                lblAvailibity.Text = "is empty";
            }
            else
            {
                if (lUserRegistrationBus.IsLoginIDExisting(txtLoginId.Text.Trim()))
                {
                    lblAvailibity.ForeColor = System.Drawing.Color.Red;
                    lblAvailibity.Text = "Not Available";
                }
                else
                {
                    lblAvailibity.ForeColor = System.Drawing.Color.Green;
                    lblAvailibity.Text = "Available";
                }
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {

                if (txtAntiSpam.Text == hidCorrectAnswer.Value)
                {
                    //true

                    User lUser = new User();
                    lUser.UserLoginID = this.txtLoginId.Text;
                    lUser.UserPassword = StringUtility.EncryptString(this.txtPassword.Text);
                    lUser.UserFirstName = this.txtFirstName.Text;
                    lUser.UserLastName = this.txtLastName.Text;
                    lUser.UserPrimaryEmailID = this.txtEmailId.Text;
                    lUser.UserVerificationCode = StringUtility.GenerateRandomString(6);

                    lUser.Sys_CreatedByID = this.txtLoginId.Text;

                    IUserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
                    if (lUserRegistrationBus.RegisterNewUser(lUser))
                    {

                        UserProfile _UserProfile = new UserProfile();
                        _UserProfile.UserKey = lUser.UserKey;
                        _UserProfile.UserFullName = lUser.UserFirstName + " " + lUser.UserLastName;
                        if (this.ddlGender.Text.Substring(0, 1) == "M" || this.ddlGender.Text.Substring(0, 1) == "F")
                        {
                            _UserProfile.UserSex = this.ddlGender.Text.Substring(0, 1);
                        }
                        _UserProfile.UserAlternateEmailID = this.txtAlternateEmailId.Text;
                        //_UserProfile.UserDateOfBirth = DateTime.Parse(txtDOB.Text.Trim());

                        AppSettingsReader _AppSettingsReader = new AppSettingsReader();
                        string _CultureInfo = _AppSettingsReader.GetValue(constCultureInfo, typeof(string)).ToString();

                        IFormatProvider _CultureFormatProvider = new System.Globalization.CultureInfo(_CultureInfo, true);
                        if (!string.IsNullOrEmpty(txtDOB.Text.Trim()))
                        {
                            _UserProfile.UserDateOfBirth = DateTime.Parse(txtDOB.Text.Trim(), _CultureFormatProvider, System.Globalization.DateTimeStyles.AssumeLocal);
                        }
                        _UserProfile.UserContactNumber = this.txtContactNo.Text;
                        _UserProfile.UserContactAddress = this.txtAddress.Text;

                        _UserProfile.Sys_CreatedByID = lUser.Sys_CreatedByID;

                        lUserRegistrationBus.SaveUserProfile(_UserProfile);
                        // Display success message
                        lUserRegistrationBus.SendVerificationCodeEmail(lUser);
                        UserContext UserSession = new UserContext();
                        //UserSession.UserLoginID = lUser.UserLoginID;
                        //UserSession.UserFirstName = lUser.UserFirstName;
                        //UserSession.UserLastName = lUser.UserLastName;
                        //UserSession.UserPrimaryEmailID = lUser.UserPrimaryEmailID;
                        //UserSession.UserVerificationCode = lUser.UserVerificationCode;
                        //UserSession.UserIsVerified = lUser.UserIsVerified;
                        UserSession.CurrentUser = lUser;
                        Session["UserContext"] = UserSession;
                        Response.Redirect("~/Pages/UserVerification.aspx", true);
                    }
                    else
                    {
                        lblAvailibity.ForeColor = System.Drawing.Color.Red;
                        lblAvailibity.Text = "Not Available";
                    }
                }

                else
                {
                    hidCorrectAnswer.Value = antiSpam.GenerateMathFormula();
                    lblAntiSpam.Text = antiSpam.MathFormula;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                //Need to display the exception message to the user or log it in application log.
            }  
        }


        #endregion Events

        #region Private Methods
        private string GetCurrentLoginUserID()
        {
            string lCurrentLoginUserID = "SysAdmin";
            try
            {
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print(ex.Message);
                //Need to display the exception message to the user or log it in application log.
            }
            return lCurrentLoginUserID;
        }
        #endregion Private Methods

        
    }
}